package com.vtc.session3;

import java.util.ArrayList;
import java.util.List;

import com.vtc.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.SearchView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class EmployeeList1Activity extends Activity implements OnClickListener , SearchView.OnQueryTextListener,SearchView.OnCloseListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
		 
		this.setContentView(R.layout.employeelist1_activity);
		ArrayList<Employee> arr = new ArrayList<Employee>();
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen B", "Male", 20));
		arr.add(new Employee("Vu Lam", "Male", 20));
		arr.add(new Employee("Hong Ngan", "FeMale", 20));
		arr.add(new Employee("Nguyen Tuan", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		arr.add(new Employee("Nguyen Van A", "Male", 20));
		 
		TableLayout layout =(TableLayout) this.findViewById(R.id.tblEmp);		
		int i=0;
		for(Employee e:arr){
			LayoutInflater inflat =  (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			TableRow row1 = (TableRow) inflat.inflate(R.layout.employeelist_rowitem,null);
            TextView tvName1 =(TextView) row1.findViewById(R.id.tvName);
            TextView tvGender1 =(TextView) row1.findViewById(R.id.tvGender);
            TextView tvAge1 =(TextView) row1.findViewById(R.id.tvAge);		

			tvName1.setText(e.getName());			
			tvGender1.setText(e.getGender());
			tvAge1.setText(e.getAge()+"");
			if(++i%2==0){
				row1.setBackgroundResource(R.layout.rowitem);
			}
			else{
				row1.setBackgroundResource(R.layout.rowitem1);
			}
			// dang ky su kien clikc vao tung rong
			row1.setOnClickListener(this);
			layout.addView(row1);
		}		
	}
	private SearchView mSearchView;
	@SuppressLint("NewApi")
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
 
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_game, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
        setupSearchView(searchItem);
 
        return true;
    }
	protected boolean isAlwaysExpanded() {
        return false;
    }
	@SuppressLint("NewApi")
	private void setupSearchView(MenuItem searchItem) {
		 
        if (isAlwaysExpanded()) {
            mSearchView.setIconifiedByDefault(false);
        } else {
            searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        }
 
//        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
//        if (searchManager != null) {
//            List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();
// 
//            SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
//            for (SearchableInfo inf : searchables) {
//                if (inf.getSuggestAuthority() != null
//                        && inf.getSuggestAuthority().startsWith("applications")) {
//                    info = inf;
//                }
//            }
//            mSearchView.setSearchableInfo(info);
//        }
 
        mSearchView.setOnQueryTextListener(this);
    }
	@Override
	public void onClick(View v) {		
		TableRow row = (TableRow) v;
		TextView tv1 = (TextView) row.getChildAt(0);
		TextView tv2 = (TextView) row.getChildAt(1);
		TextView tv3 = (TextView) row.getChildAt(2);
		
		String data = tv1.getText().toString()+">>"+tv2.getText().toString()+">>"+tv3.getText().toString();
		Toast.makeText(this, data, Toast.LENGTH_LONG).show();
	}
	@SuppressLint("NewApi")
	@Override
	public boolean onQueryTextChange(String newText) {
		 Toast.makeText(this,"Query = " + newText,Toast.LENGTH_LONG).show();
		 
		return false;
	}
	@SuppressLint("NewApi")
	@Override
	public boolean onQueryTextSubmit(String newText) {
		Toast.makeText(this,"Query = " + newText+" submit",Toast.LENGTH_LONG).show();
		
		return false;
	}
	@Override
    public boolean onClose() {       
        return false;
    }
	
}
